package cn.zyjblogs.server.user.service.impl;

import cn.zyjblogs.server.user.mapper.UserRoleMapper;
import cn.zyjblogs.server.user.po.UserRolePo;
import cn.zyjblogs.server.user.service.UserRoleService;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuyijun
 */
@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRolePo> implements UserRoleService {
    private final UserRoleMapper userRoleMapper;

    /**
     * 保存或用户角色关联
     *
     * @param userId    用户id
     * @param roleIds   角色id集合
     * @param needClear 是否清除该用户旧关联
     * @author zhuyijun
     * @date 2022/9/23 下午11:10
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdate(String userId, Collection<String> roleIds, boolean needClear) {
        if (needClear) {
            deleteByUserId(userId);
        }
        List<UserRolePo> userRolePos = new ArrayList<>(roleIds.size());
        String tenantId = BaseContext.getTenantId();
        roleIds.forEach(roleId -> userRolePos.add(UserRolePo.builder()
                .roleId(roleId)
                .userId(userId)
                .tenantId(tenantId)
                .build()));

        return this.saveBatch(userRolePos);
    }

    /**
     * 通过用户id集合删除用户角色关联表
     *
     * @param userId 用户id
     * @author zhuyijun
     * @date 2022/9/23 下午11:00
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByUserId(String userId) {
        LambdaQueryWrapper<UserRolePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRolePo::getUserId, userId)
                .eq(UserRolePo::getTenantId, BaseContext.getTenantId());
        return userRoleMapper.delete(queryWrapper);
    }

    /**
     * 通过用户id集合删除用户角色关联表
     *
     * @param userIds 用户id集合
     * @author zhuyijun
     * @date 2022/9/23 下午11:00
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByUserIds(Collection<String> userIds) {
        LambdaQueryWrapper<UserRolePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(UserRolePo::getUserId, userIds)
                .eq(UserRolePo::getTenantId, BaseContext.getTenantId());
        return userRoleMapper.delete(queryWrapper);
    }

    /**
     * 通过用户id查询角色id集合
     *
     * @param userId 用户id
     * @author zhuyijun
     * @date 2022/9/23 下午11:00
     */
    @Override
    public List<String> findRoleListByUserId(String userId) {
        LambdaQueryWrapper<UserRolePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(UserRolePo::getRoleId)
                .eq(UserRolePo::getUserId, userId)
                .eq(UserRolePo::getTenantId, BaseContext.getTenantId());
        List<UserRolePo> userRolePos = userRoleMapper.selectList(queryWrapper);
        return userRolePos.stream().map(UserRolePo::getRoleId).collect(Collectors.toList());
    }
}
